<?php

/* * ***********************************************************************
  Class Pick_Core_Upload
  ----------------------------------------------------------------
  Derasoft CMS Project
  Company: Derasoft Co., Ltd
  Name: Vo Quoc Nguyen
  Last updated: 17/10/2009
 * ************************************************************************ */

class Pick_Core_Upload {

    var $uploadDir;
    var $fileName;
    var $fileSize;
    var $tempLocation;
    var $maxFileSize;
    var $type;

    /* -----------------------------------------------------------------------*
     * Function: Constructor
     * Parameter: DB , Table, Fields
     * Return: No return
     * -----------------------------------------------------------------------da */

    function Pick_Core_Upload($files = '') {
        if ($files != '') {
            $this->fileName = $files['name'];
            $this->fileSize = $files['size'];
            $this->tempLocation = $files['tmp_name'];
            $thum = substr($this->fileName, -4, 4);
            if ($thum == 'jpeg') {
                $thum = '.jpeg';
                $nametamp = substr($this->fileName, 0, -4);
                $name = Pick_Data_TextFilter::urlize($nametamp, false, '-');
                $this->type = strtolower(substr($this->fileName, -4));
            } else {
                $nametamp = substr($this->fileName, 0, -3);
                $name = Pick_Data_TextFilter::urlize($nametamp, false, '-');
                $this->type = strtolower(substr($this->fileName, -3));
            }
            $this->name = addslashes(rand() . "_" . $name . $thum);
        }
    }

    function moveImage($uploadDir = "", $nameFileOld = "", $resize = false, $sizeimages = array()) {
        $gallery_avatar = $uploadDir . "avatar";
        $gallery_medium = $uploadDir . "medium";
        $gallery_big = $uploadDir . "big";
        $gallery_storage = $uploadDir . "storage";
        $tmp_file = $this->tempLocation;
        if ($this->checkType() == "" && !move_uploaded_file($tmp_file, $gallery_storage . "/" . $this->name)) {
            return false;
        }
        if (!empty($nameFileOld)) {
            unlink($gallery_avatar . "/" . $nameFileOld);
            unlink($gallery_medium . "/" . $nameFileOld);
            unlink($gallery_big . "/" . $nameFileOld);
            unlink($gallery_storage . "/" . $nameFileOld);
        }
        if ($resize) {
            $this->resize($gallery_storage, $gallery_avatar, $sizeimages['wavatar'], $sizeimages['havatar']);
            $this->resize($gallery_storage, $gallery_medium, $sizeimages['wmedium'], $sizeimages['hmedium']);
            $this->resize($gallery_storage, $gallery_big, $sizeimages['wbig'], $sizeimages['hbig']);
        }
        return $this->name;
    }

    function moveFile($uploadDir = "", $nameFileOld = "") {
        $tmp_file = $this->tempLocation;
        if ($this->checkType() == "" && !move_uploaded_file($tmp_file, $uploadDir . "/" . $this->name)) {
            return false;
        }
        if (!empty($nameFileOld)) {
            unlink($uploadDir . "/" . $nameFileOld);
        }
        return $this->name;
    }

    public static function getFilePath($file){
        return base_url().'/upload/files/'.$file;
    }

    public static function getImagePath($file, $type = "storage"){
        return base_url().'/upload/images/'.$type.'/'.$file;
    }
    function checkType() {
        if (preg_match("/" . ALLOW_FILE_TYPES . "/", strtolower($this->type))) {
            return '1';
        }
        return "";
    }

    function getType() {
        return $this->type;
    }

    function getNameFile() {
        return $this->name;
    }

    #SetMaxFileSize

    function setMaxFileSize($size, $sizeType = UPLOAD_SIZE_BYTES) {
        if ($sizeType == UPLOAD_SIZE_BYTES) {
            $bytes = $this->toBytes($size);
            $this->maxFileSize = $bytes;
        } else {
            $this->maxFileSize = intval($size);
        }
    }

    #CheckSize

    function checkSize() {
        $maxFileSize = $this->maxFileSize;
        $fileSize = $this->fileSize;
        if ($fileSize > $maxFileSize) {
            return false;
        } else {
            return true;
        }
    }

    function getWidth($uploadDir) {
        $filepath = $uploadDir . $this->name;
        $size_info = getimagesize($filepath);
        return $size_info[0];
    }

    function getHight($uploadDir) {
        $filepath = $uploadDir . $this->name;
        $size_info = getimagesize($filepath);
        return $size_info[1];
    }

    function getPhenotype($uploadDir) {
        $filepath = $uploadDir . $this->name;
        $size_info = getimagesize($filepath);
        if ($size_info[0] > $size_info[1])
            return 1; // hinh nam
        if ($size_info[0] < $size_info[1])
            return 2; // hinh dung
        return 0; // hinh vuong
    }

    //funtion resize
    function resize($uploadDir, $thumb_path, $width = 0, $hight = 0, $square = 0, $quality = 90) {
        $image_path = $uploadDir;
        $checkimage = $this->getPhenotype($uploadDir);
        $width_size = ($this->getWidth($uploadDir) * $hight) / $this->getHight($uploadDir);
        $hight_size = ($this->getHight($uploadDir) * $width) / $this->getWidth($uploadDir);
        if ($checkimage == 1) {//hinh nam
            if ($width_size > $width) {
                $dimension = $width;
                if ($hight_size > $hight)
                    $dimension = $hight * $this->getWidth($uploadDir) / $this->getHight($uploadDir);
            }else {
                if ($hight_size > $hight)
                    $dimension = $hight * $this->getWidth($uploadDir) / $this->getHight($uploadDir);
                else
                    $dimension = $width;
            }
        }else {
            if ($checkimage == 2) {// hinh dung
                if ($hight_size > $hight)
                    $dimension = $hight;
                else
                if ($width_size > $width)
                    $dimension = ($width * $this->getHight($uploadDir)) / $this->getWidth($uploadDir);
                else
                    $dimension = $hight;
            }else {
                $dimension = $width;
            }
        }
        $image_name = $this->name;
        $type = strtolower(substr($image_name, -3));
        switch ($type) {
            case 'jpg':
                $src = imagecreatefromjpeg("$image_path/$image_name");
                break;
            case 'gif':
                $src = imagecreatefromgif("$image_path/$image_name");
                break;
            case 'png':
                $src = imagecreatefrompng("$image_path/$image_name");
                break;
            case 'peg':
                $src = imagecreatefromjpeg("$image_path/$image_name");
                break;
            case 'bmp':
                $src = imagecreatefromjpeg("$image_path/$image_name");
                break;
        }
        $ow = imagesx($src);
        $oh = imagesy($src);
        $src_x = 0;
        $src_y = 0;
        if ($ow > $oh) {
            if ($ow > $dimension) {
                $nw = $dimension;
                $nh = (int) $oh * $nw / $ow;
            } else {
                $nw = $ow;
                $nh = (int) $oh * $nw / $ow;
            }
        } else {
            if ($oh > $dimension) {
                $nh = $dimension;
                $nw = (int) $ow * $nh / $oh;
            } else {
                $nh = $oh;
                $nw = (int) $ow * $nh / $oh;
            }
        }
        if ($square) {
            $length = min($ow, $oh);
            $src_x = ceil($ow / 2) - ceil($length / 2);
            $src_y = ceil($oh / 2) - ceil($length / 2);
            $nlength = max($nw, $nh);
            $nw = $nlength;
            $nh = $nlength;
            $ow = $length;
            $oh = $length;
        }
        $dst = imagecreatetruecolor($nw, $nh);
        imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $nw, $nh, $ow, $oh);
        imagejpeg($dst, "$thumb_path/$image_name", $quality);
        imagedestroy($src);
        imagedestroy($dst);
        return true;
    }

    function watermark($uploadDir, $watemark_path, $watemark_name, $quality = 90) {
        $image_path = $uploadDir . $this->name;
        $checkimage = $this->getPhenotype($uploadDir);
        if ($checkimage == 1) {//hinh nam
            $imgwidth = $this->getWidth($uploadDir);
            $watemark_name . '_nam.png';
            $logo_path = $watemark_path . $watemark_name;
            $watwidth = imagesx($logo_path);
            if ($imgwidth > $watwidth)
                $this->resize($uploadDir, $uploadDir, 800, 600);
            else {
                $imgHight = $this->getHight($uploadDir);
                $this->resize($watemark_path, $watemark_path, $watWidth, $imgHight);
                $logo_path = $watemark_path . $this->name;
            }
        } else {
            if ($checkimage == 2) {// hinh dung
                $imgHight = $this->getHight($uploadDir);
                $watemark_name . '_dung.png';
                $logo_path = $watemark_path . $watemark_name;
                $watHight = imagesy($logo_path);
                ;
                if ($imgHight > $watHight) {
                    $this->resize($uploadDir, $uploadDir, 600, 800);
                } else {
                    $imgWidth = $this->getWidth($uploadDir);
                    $this->resize($watemark_path, $watemark_path, $watWidth, $imgHight);
                    $logo_path = $watemark_path . $this->name;
                }
            }
        }

        $resultImage = imagecreatefromjpeg($image_path);
        imagealphablending($resultImage, TRUE);

        $finalWaterMarkImage = imagecreatefrompng($logo_path);
        $finalWaterMarkWidth = imagesx($finalWaterMarkImage);
        $finalWaterMarkHeight = imagesy($finalWaterMarkImage);

        imagecopy($resultImage, $finalWaterMarkImage, 0, 0, 0, 0, $finalWaterMarkWidth, $finalWaterMarkHeight);

        imagejpeg($resultImage, $image_path, $quality);

        imagedestroy($resultImage);
        imagedestroy($finalWaterMarkImage);
    }

#GetSize File	 

    function getFileSize($sizeType = UPLOAD_SIZE_BYTES) {
        $bytes = $this->fileSize;
        if ($sizeType == UPLOAD_SIZE_BYTES)
            return $this->toBytes($bytes);
        return $bytes;
    }

    /* -----------------------------------------------------------------------*
     * Function: toMBytes
     * Parameter: bytes, decimal
     * Constants:  UPLOAD_SIZE_MBYTES | UPLOAD_SIZE_BYTES
     * Return: file size 
     * ----------------------------------------------------------------------- */

    function toMBytes($bytes, $decimal = 2) {
        $kb = ($bytes * 8) / 1024;
        $mb = ($kb / 1024) / 8;
        return round($mb, $decimal);
    }

    function toKBytes($bytes, $decimal = 2) {
        $kb = ($bytes * 1) / 1024;
        return round($kb, $decimal);
    }

    /* -----------------------------------------------------------------------*
     * Function: toBytes
     * Parameter: Mega bytes
     * Constants:  UPLOAD_SIZE_MBYTES | UPLOAD_SIZE_BYTES
     * Return: file size 
     * ----------------------------------------------------------------------- */

    function toBytes($mb) {
        $bytes = ((($mb * 8) * 1024) * 1024) / 8;
        return $bytes;
    }

    function isImage($str) {
        $ext = strtolower(substr($str, -3));
        if (preg_match("/jpg|png|bmp|gif|peg/", $ext))
            return 1;
        return 0;
    }

    function isVideo($str) {
        $ext = strtolower(substr($str, -3));
        if (preg_match("/wmv|mpg|mp4|mov|avi/", $ext))
            return 1;
        return 0;
    }

    function isMusic($str) {
        $ext = strtolower(substr($str, -3));
        if (preg_match("/wma|wav|mp3|asf|/", $ext))
            return 1;
        return 0;
    }

    function isFile($str) {
        $ext = strtolower(substr($str, -3));
        if (preg_match("/doc|zip|rar|/", $ext))
            return 1;
        return 0;
    }

}

?>